/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include <hal_delay.h>
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_compiler.h>
#include <x_config.h>

#ifdef CONFIG_OS_PONY
#include <k_tick.h>
#endif

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

__weak void hal_delay_us(uint32_t us)
{
    // Assume the CPU frequency is 100 MHz
    volatile uint32_t v = us * 100;
    while (v--) {
    }
}

__weak void hal_delay_ms(uint32_t ms)
{
#ifdef CONFIG_OS_PONY
    uint32_t start = tick_ms();
    uint32_t wait = ms;

    while ((tick_ms() - start) < wait) {
    }
#else
    hal_delay_us(ms * 1000);
#endif
}
